import { type NextRequest, NextResponse } from "next/server"
import { getDb } from "@/lib/db-sqlite"

// 删除用户
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
  try {
    const { id } = await params

    // 防止删除默认管理员
    const db = getDb()
    const user = db.prepare("SELECT name FROM users WHERE id = ?").get(id)

    if (user && user.name === "admin") {
      return NextResponse.json({ error: "Cannot delete default admin user" }, { status: 403 })
    }

    const result = db
      .prepare(`
        DELETE FROM users
        WHERE id = ?
      `)
      .run(id)

    if (result.changes === 0) {
      return NextResponse.json({ error: "User not found" }, { status: 404 })
    }

    return NextResponse.json({ success: true })
  } catch (error: any) {
    console.error("Error deleting user:", error)
    return NextResponse.json({ error: error.message || "Failed to delete user" }, { status: 500 })
  }
}
